﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utils.Extention;
#region Version Info
/* ========================================================================
* 【本页面功能概述】
*
* 作者：wangjianfeng 创建时间：2012/9/16 15:34:01
* 文件名：NewsChoose.cs
*
* 修改者： 时间：
* 修改说明：
* ========================================================================
*/
#endregion
namespace ChinaLeg.Web.WebAdmin.News
{
    public partial class NewsChoose : ChinaLeg.Web.Lib.PageBase
    {
        protected string CatName;
        protected int CatId;
        protected int NewsId;
        protected void Page_Load(object sender, EventArgs e)
        {
            CatId = Request.QueryString["id"].ToInt32();
            CatName = Request.QueryString["name"].ToTrim();
            NewsId = Request.QueryString["newsid"].ToInt32();
            txtDate.Attributes["onclick"] = "__Calendar__.show(this, {})";
            if (!this.IsPostBack)
            {
                BindList();
                lbList.DataSource = ChinaLeg.BLL.News.Instance.GetList(NewsId);
                lbList.DataTextField = "Title";
                lbList.DataValueField = "Id";
                lbList.DataBind();
            }
        }
        public void BindList()
        {
            string where = " and catid=" + CatId;

            switch (ddlCommend.SelectedValue)
            {
                case "1":
                    where += " and IndexCommend=1 ";
                    break;
                case "2":
                    where += " and ListCommend=1 ";
                    break;
                case "3":
                    where += " and DetailsCommend=1 ";
                    break;
            }

            if (!Request.Form["txtDate"].ToTrim().IsNullOrEmpty())
            {
                where += string.Format(" and MidifyTime between '{0}' and '{0} 23:59:59' ", Request.Form["txtDate"]);
            }

            if (!txtTitle.Text.ToTrim().IsNullOrEmpty())
            {
                where += string.Format(" and Title like '%{0}%' ", txtTitle.Text);
            }

            int recordCount;
            gvData.DataSource = ChinaLeg.BLL.News.Instance.GetList(where, aspNetPager.PageSize, aspNetPager.CurrentPageIndex, out recordCount);
            gvData.DataBind();
            aspNetPager.RecordCount = recordCount;
        }
        protected void aspNetPager_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            aspNetPager.CurrentPageIndex = 1;
            BindList();

        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
           
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            int len = gvData.Rows.Count;
            for (int i = 0; i < len; i++)
            {
                if ((gvData.Rows[i].FindControl("chk") as CheckBox).Checked)
                {
                    int relnewsid = gvData.Rows[i].Cells[1].Text.ToInt32();
                    string text = (gvData.Rows[i].FindControl("litTitle") as Literal).Text;

                    ListItem li = new ListItem(text, relnewsid.ToString());
                    if (!lbList.Items.Contains(li))
                    {
                        ChinaLeg.BLL.News.Instance.Add(NewsId, relnewsid);
                        lbList.Items.Add(li);
                    }
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int len = lbList.Items.Count;
            for (int i = len-1; i >=0; i--)
            {
                if (lbList.Items[i].Selected)
                {
                    int relnewsid = lbList.Items[i].Value.ToInt32();
                    
                    ChinaLeg.BLL.News.Instance.Delete(NewsId, relnewsid);

                    lbList.Items.RemoveAt(i);
                }
            }
        }
    }
}